/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mapeamento;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import negocio.Column;
import negocio.ResultCalcDataBase;
import negocio.Table;
import persistencia.FacadePersistence;

/**
 *
 * @author Angelo
 */
public class MapperDTO {

    private FacadePersistence fp;

    public MapperDTO() {
        fp = new FacadePersistence();
    }

    public void connect(String usuario, String senha, String host, int porta, String sid) throws SQLException, ClassNotFoundException {
        fp.connect(usuario, senha, host, porta, sid);
    }

    public List<String> getOwners() throws SQLException {
        return fp.getOwners();
    }
    
    public boolean createStructure() throws SQLException
    {
        return fp.createStructure();
    }
            
    public boolean reCreateStructure(String owner) throws SQLException
    {
        return fp.reCreateStructure(owner);
    }
    
    public boolean hasStructure(String owner) throws SQLException
    {
        return fp.hasStructure(owner);
    }
    
    public void setOwner(String owner) throws SQLException
    {
        fp.setOwner(owner);
    }

//    public List<Table> carregaDados(String owner) throws SQLException {
//        List<Table> l = new ArrayList<Table>();
//        Table tbl = null;
//        Column col;
//        String[] v;
//        for (String t : fp.getTables(owner)) {
//            tbl = new Table(t);
//            for (String a : fp.getColumns(owner, t)) {
//                v = a.split(";");
//                for (int i = 0; i < v.length; i++) {
//                    if (v[i].equals("null")) {
//                        v[i] = "0";
//                    }
//                }
//                col = new Column(v[0], v[1], Integer.parseInt(v[2]), Integer.parseInt(v[3]), v[4]);
//                tbl.addColumn(col);
//            }
//            l.add(tbl);
//        }
//        return l;
//    }    
    public List<Table> loadData(String owner) throws SQLException {
        List<Table> l = new ArrayList<Table>();
        Table tbl;
        Column col;
        String[] v;

        for (String t : fp.getTables(owner)) {
            v = t.split(";");
            for (int i = 0; i < v.length; i++) {
                if (v[i].equals("null")) {
                    v[i] = "0";
                }
            }
            tbl = new Table(v[0], Integer.parseInt(v[1]), Double.parseDouble(v[2]), Integer.parseInt(v[3]));
            for (String a : fp.getColumns(owner, tbl.getTable_name())) {
                v = a.split(";");
                for (int i = 0; i < v.length; i++) {
                    if (v[i].equals("null")) {
                        v[i] = "0";
                    }
                }
                col = new Column(v[0], v[1], Integer.parseInt(v[2]), Integer.parseInt(v[3]), v[4], Double.parseDouble(v[5]), Double.parseDouble(v[6]));
                tbl.addColumn(col);
            }
            l.add(tbl);
        }
        return l;
    }

    public void writeDataTables(String owner, String table, int records, double growth, int retention) throws SQLException {
        fp.writeDataTables(owner, table, records, growth, retention);
    }

    public void writeDataColumn(String owner, String table, String column, double avg, double percent) throws SQLException {
        fp.writeDataColumns(owner, table, column, avg, percent);
    }

//    public void restultCalc(String owner) {
//        //chama procedures do banco e retorna o resultado da simulação.
//    }

        
    public void calcInitialOcupation(String table_name, int numberOfLines) throws SQLException 
    {
        fp.calcInitialOcupation(table_name, numberOfLines);
    }

    public void cleanTablesOfValumetricCalculation() throws SQLException 
    {
        fp.cleanTablesOfValumetricCalculation();
    }
    
    public List<String> restultCalc() throws SQLException
    {
        return fp.restultCalc();
    }
}
